Elaboration Iteration 1


Domain Model


classDiagram.PNG

Model-to-Code


Sign-In Class

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
 
namespace Sudoku_Game
{
    public partial class SignIn : Form
    {
 
        private string userID;
        private Sudoku instance;
        public SignIn(Sudoku instance)
        {
            InitializeComponent();
            this.instance = instance;
        }
 
        private void signInButton_Click(object sender, EventArgs e)
        {
            string username = usernameTB.Text;
            string password = passwordTB.Text;
            if (LogIn(username, password))
            {
 
                instance.CurrentUser = UserStatus.Player;
                instance.UserID = this.userID;
                this.Close();
            }
            else
            {
                MessageBox.Show("Invalid Login. Try Again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
 
        }
 
        private void guestButton_Click(object sender, EventArgs e)
        {
            instance.CurrentUser = UserStatus.Guest;
            this.Close();
        }
 
        private bool LogIn(string username, string password)
        {
            bool loggedIn = false;
            SqlCeConnection conn = new SqlCeConnection(@"Data Source=C:\Users\Tim\Documents\My Dropbox\Sudoku Game\Sudoku Game\Sudoku.sdf");
            conn.Open();
            SqlCeCommand cmd = new SqlCeCommand("Select password, userId FROM Users WHERE username='" + username + "'", conn);
            SqlCeDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                if (rdr[0].ToString() == password)
                {
                    loggedIn = true;
                    userID = rdr[1].ToString();
                }
            }
            conn.Close();
            return loggedIn;
 
        }
 
    }
}
 

Sudoku Class

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
 
namespace Sudoku_Game
{
    public partial class Sudoku : Form
    {
        DataTable table;
        Grid currentGrid;
        UserStatus currentUser;
        string userID;
 
        #region Properties
        public string UserID
        {
            get { return userID; }
            set { userID = value; }
        }
 
        public UserStatus CurrentUser
        {
            get { return currentUser; }
            set { currentUser = value; }
        }
        #endregion
 
        #region Constructor
        public Sudoku()
        {
            InitializeComponent();
            SignIn form = new SignIn(this);
            form.ShowDialog();
            if (currentUser == UserStatus.Guest)
            {
                this.loadButton.Enabled = false;
                this.newButton.Enabled = false;
                PopulateBlankGrid();
            }
            else
            {
                this.loadButton.Enabled = true;
                this.newButton.Enabled = true;
            }
            dataGridView1.CellToolTipTextNeeded += new DataGridViewCellToolTipTextNeededEventHandler(dataGridView1_CellToolTipTextNeeded);
        }
        #endregion
 
        #region Methods
 
        private void PopulateBlankGrid()
        {
            currentGrid = new Grid();
            this.SetupDataTable();
        }
 
        private void SetupDataTable()
        {
            this.dataGridView1.DataSource = null;
            table = new DataTable();
            for (int i = 0; i < 9; i++)
                table.Columns.Add(i.ToString());
            for (int i = 0; i < 9; i++)
                table.Rows.Add("");
            this.dataGridView1.DataSource = table;
            foreach (DataGridViewColumn col in this.dataGridView1.Columns)
                col.Width = 22;
            foreach (DataGridViewRow row in this.dataGridView1.Rows)
                row.Height = 22;
 
        }
 
        private void PopulateRandomGrid()
        {
 
        }
 
        private void GenerateHint()
        {
            foreach (Cell cell in this.currentGrid.Cells)
            {
                if (this.currentGrid.GetAvailableNumbers(cell).Count == 1)
                {
                    int value = this.currentGrid.GetAvailableNumbers(cell)[0];
                    SetValue(cell.Row, cell.Column, value);
                    table.Rows[cell.Row][cell.Column] = value;
                    return;
                }
            }
            //DO SOME GUESSING
        }
 
        private void GenerateSolution()
        {
            foreach (Cell cell in this.currentGrid.Cells)
            {
                if (this.currentGrid.GetAvailableNumbers(cell).Count == 1)
                {
                    int value = this.currentGrid.GetAvailableNumbers(cell)[0];
                    SetValue(cell.Row, cell.Column, value);
                    table.Rows[cell.Row][cell.Column] = value;
                }
            }
        }
 
        private void LoadUserGrid()
        {
            string board = "";
            SqlCeConnection conn = new SqlCeConnection(@"Data Source=C:\Users\Tim\Documents\My Dropbox\Sudoku Game\Sudoku Game\Sudoku.sdf");
            conn.Open();
            SqlCeCommand cmd = new SqlCeCommand("Select savedGame FROM data_storage WHERE userid='" + this.UserID + "' ORDER BY dateTime DESC", conn);
            SqlCeDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                board = rdr[0].ToString();
                break;
            }
            conn.Close();
 
            if (board != "")
            {
 
                this.currentGrid = new Grid();
                this.SetupDataTable();
                string[] cells = board.Split(' ');
                int count = 0;
                for (int row = 0; row < 9; row++)
                {
                    for (int col = 0; col < 9; col++)
                    {
                        this.currentGrid.GetCellAt(row, col).Value = int.Parse(cells[count]);
                        if (cells[count] != "0")
                            this.table.Rows[row][col] = cells[count];
                        count++;
                    }
                }
 
 
            }
            else
            {
                MessageBox.Show("No Saved Data.");
            }
 
        }
 
        private void SaveCurrentGrid()
        {
            string board = "";
            foreach (Cell cell in this.currentGrid.Cells)
            {
                board += cell.Value + " ";
            }
 
            SqlCeConnection conn = new SqlCeConnection(@"Data Source=C:\Users\Tim\Documents\My Dropbox\Sudoku Game\Sudoku Game\Sudoku.sdf");
            conn.Open();
            SqlCeCommand cmd = new SqlCeCommand("Insert INTO data_storage (userid, savedGame, dateTime) VALUES ('" + userID + "', '" + board + "' , '" + DateTime.Now + "')", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
 
        }
 
        #endregion
 
        #region Events
 
        private void loadButton_Click(object sender, EventArgs e)
        {
            this.LoadUserGrid();
        }
 
        private void newButton_Click(object sender, EventArgs e)
        {
            this.PopulateRandomGrid();
        }
 
        private void blankButton_Click(object sender, EventArgs e)
        {
            this.PopulateBlankGrid();
        }
 
        private void clear_Click(object sender, EventArgs e)
        {
            this.messageBox.Text = "";
        }
 
        private void SolveButton_Click(object sender, EventArgs e)
        {
            GenerateSolution();
        }
 
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            var col = e.ColumnIndex;
            var row = e.RowIndex;
            var val = table.Rows[row][col].ToString();
            int value;
            if (int.TryParse(val, out value))
            {
                if (this.currentGrid.GetCellAt(row, col).Value != value)
                    this.currentGrid.GetCellAt(row, col).Value = 0;
 
                this.SetValue(row, col, value);
 
            }
            else
            {
                table.Rows[row][col] = "";
                messageBox.Text += "\n" + val + " is invalid.";
            }
 
        }
 
        private bool SetValue(int row, int col, int value)
        {
            Cell cell = currentGrid.GetCellAt(row, col);
            if (value <= 9 && value >= 1)
            {
                if (!cell.ReadOnly)
                {
                    if (this.currentGrid.GetAvailableNumbers(cell).Contains(value))
                    {
                        cell.Value = value;
                        messageBox.Text += "\n" + value + " added for cell " + row + "," + col;
                        return true;
                    }
                    else
                    {
                        table.Rows[row][col] = "";
                        messageBox.Text += "\n" + value + " is invalid.";
                        return false;
 
                    }
                }
                else
                {
                    table.Rows[row][col] = "";
                    messageBox.Text += "\n" + " is invalid.";
                    return false;
                }
            }
            return false;
 
        }
 
        private void hintButton_Click(object sender, EventArgs e)
        {
            GenerateHint();
 
        }
 
        #endregion
 
        private void Sudoku_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (CurrentUser == UserStatus.Player)
                SaveCurrentGrid();
        }
 
 
 
 
 
        void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
        {
            Cell cell = this.currentGrid.GetCellAt(e.RowIndex, e.ColumnIndex);
            e.ToolTipText = "Value: "+ cell.Value+" Available: ";
            foreach (int c in this.currentGrid.GetAvailableNumbers(cell))
                e.ToolTipText += c + " ";
 
        }
 
 
    }
 
 
}
 

Grid Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
 
namespace Sudoku_Game
{
    class Grid
    {
        private List<Cell> cells;
 
        internal List<Cell> Cells
        {
            get { return cells; }
            set { cells = value; }
        }
 
        public Grid()
        {
            this.cells = new List<Cell>();
            for (int row = 0; row < 9; row++)
            {
                for (int col = 0; col < 9; col++)
                {
                    Cell cell = new Cell(row, col);
                    this.cells.Add(cell);
                }
            }
        }
 
        public Cell GetCellAt(int row, int col)
        {
            return this.cells.Find((Cell x) => x.Column == col && x.Row == row);
        }
 
        public List<int> GetAvailableNumbers(Cell cell)
        {
            if (cell.Value != 0)
                return new List<int>();
            List<int> availableNums = new List<int>() {1,2,3,4,5,6,7,8,9};
            foreach (Cell c in GetCellsInRow(cell.Row))
                availableNums.Remove(c.Value);
            foreach (Cell c in GetCellsInColumn(cell.Column))
                availableNums.Remove(c.Value);
            foreach (Cell c in GetCellsInSubSquare(cell.SubSquare))
                availableNums.Remove(c.Value);
            return availableNums;
 
        }
 
        private List<Cell> GetCellsInRow(int row)
        {
            List<Cell> list = new List<Cell>();
            foreach (Cell cell in this.cells)
            {
                if (cell.Row == row)
                    list.Add(cell);
            }
            return list;
        }
 
        private List<Cell> GetCellsInColumn(int col)
        {
            List<Cell> list = new List<Cell>();
            foreach (Cell cell in this.cells)
            {
                if (cell.Column == col)
                    list.Add(cell);
            }
            return list;
        }
 
        private List<Cell> GetCellsInSubSquare(int subSquare)
        {
            List<Cell> list = new List<Cell>();
            foreach (Cell cell in this.cells)
            {
                if (cell.SubSquare == subSquare)
                    list.Add(cell);
            }
            return list;
        }
    }
}
 

Cell Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
 
namespace Sudoku_Game
{
    class Cell
    {
        private int row;
        private int column;
        private int value;
        private int subsquare;
        private bool readOnly;
 
        #region Properties
        public int Row
        {
            get { return row; }
            set { row = value; }
        }
 
        public int Column
        {
            get { return column; }
            set { column = value; }
        }
 
        public int SubSquare
        {
            get { return subsquare; }
            set { subsquare = value; }
        }
 
        public int Value
        {
            get { return this.value; }
            set
            {
                if (value != 0)
                {
                    this.value = value;
                }
 
 
 
            }
        }
 
        public bool ReadOnly
        {
            get { return readOnly; }
            set { readOnly = value; }
        }
 
        #endregion
 
        #region Constructors
 
        public Cell(int row, int col)
        {
            this.Row = row;
            this.Column = col;
            this.SubSquare = calculateSubSquare();
            this.Value = 0;
            this.readOnly = false;
 
        }
 
        public Cell(int row, int col, int value)
        {
            this.Row = row;
            this.Column = col;
            this.SubSquare = calculateSubSquare();
            this.Value = value;
            this.readOnly = true;
        }
 
        #endregion
 
        #region Methods
 
        private int calculateSubSquare()
        {
            if (row <= 2)
            {
                if (column <= 2)
                    return 1;
                if (column > 2 && column <=5)
                    return 2;
                if (column > 5)
                    return 3;
            }
            if (row > 2 && row <= 5)
            {
                if (column <= 2)
                    return 4;
                if (column > 2 && column <= 5)
                    return 5;
                if (column > 5)
                    return 6;
            }
            if (row > 5)
            {
                if (column <= 2)
                    return 7;
                if (column > 2 && column <= 5)
                    return 8;
                if (column > 5)
                    return 9;
            }
            return 0;
 
        }
        #endregion
    }
}
 


UserStatus Enum
    public enum UserStatus
    {
        Player,
        Guest
    }